/**
 * 作者: wgx
 * 日期: 2025-05-06 15:25:54
 * 备注: 用户大世界事件坑位数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserWorldEventLocation;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserWorldEventLocationModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.location_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 用户Id
    protected int locationId; // 坑位id，从1-8，最多8个坑位
    protected int eventId; // 事件id (对应world_event表的id)
    protected long startTime; // 事件的刷新时间
    protected long forces; // 剩余的怪兽战力
    protected int monsterLevel; // 怪兽等级
    protected int distance; // 行军距离，来回需要X2
    protected int onewayTime; // 单程时间（秒）
    protected int failNum; // 失败次数，失败多次只降级一次
    protected long totalForces; // 初始时的总战力 (>0表示需要战斗)
    protected long eventQueueDbid; // 刷出来的事件唯一id (用来判断行军是否有效)
    protected String randomReward = ""; // 随机奖励，生成时就要确定 (如搜救事件的碎片)
    protected int type; // 事件来源类型 (0-系统整点自动刷新。1-使用道具额外刷新,5-悬赏事件，6-紧急事件7世界boss)
    protected String boxReward = ""; // 全胜时的宝箱奖励，生成时就要确定

    public void reset() {
    }

    protected String getKey() {
        return "UserWorldEventLocation." + userId + "." + locationId;
    }

    protected String getListKey() {
        return "UserWorldEventLocation." + userId;
    }

    protected String getListItemKey() {
        return locationId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_world_event_location"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 用户Id
        obj.put("locationId", locationId); // 坑位id，从1-8，最多8个坑位
        obj.put("eventId", eventId); // 事件id (对应world_event表的id)
        obj.put("startTime", startTime); // 事件的刷新时间
        obj.put("forces", forces); // 剩余的怪兽战力
        obj.put("monsterLevel", monsterLevel); // 怪兽等级
        obj.put("distance", distance); // 行军距离，来回需要X2
        obj.put("onewayTime", onewayTime); // 单程时间（秒）
        obj.put("failNum", failNum); // 失败次数，失败多次只降级一次
        obj.put("totalForces", totalForces); // 初始时的总战力 (>0表示需要战斗)
        obj.put("eventQueueDbid", eventQueueDbid); // 刷出来的事件唯一id (用来判断行军是否有效)
        obj.put("randomReward", randomReward); // 随机奖励，生成时就要确定 (如搜救事件的碎片)
        obj.put("type", type); // 事件来源类型 (0-系统整点自动刷新。1-使用道具额外刷新,5-悬赏事件，6-紧急事件7世界boss)
        obj.put("boxReward", boxReward); // 全胜时的宝箱奖励，生成时就要确定
        return obj;
    }

    public PBUserWorldEventLocation.Builder takeInitBuilder() {
        PBUserWorldEventLocation.Builder sb = PBUserWorldEventLocation.newBuilder();
        if (locationId != 0) {
            sb.setLocationId(locationId);
        }
        if (eventId != 0) {
            sb.setEventId(eventId);
        }
        if (startTime != 0) {
            sb.setStartTime(startTime);
        }
        if (forces != 0) {
            sb.setForces(forces);
        }
        if (monsterLevel != 0) {
            sb.setMonsterLevel(monsterLevel);
        }
        if (distance != 0) {
            sb.setDistance(distance);
        }
        if (onewayTime != 0) {
            sb.setOnewayTime(onewayTime);
        }
        if (totalForces != 0) {
            sb.setTotalForces(totalForces);
        }
        if (eventQueueDbid != 0) {
            sb.setEventQueueDbid(eventQueueDbid);
        }
        if (!randomReward.isEmpty()) {
            sb.setRandomReward(randomReward);
        }
        if (type != 0) {
            sb.setType(type);
        }
        if (!boxReward.isEmpty()) {
            sb.setBoxReward(boxReward);
        }
        return sb;
    }

    public PBUserWorldEventLocation.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserWorldEventLocation.Builder sb = PBUserWorldEventLocation.newBuilder();
        if (isFieldUpdate("locationId")) {
            sb.setLocationId(locationId);
        }
        if (isFieldUpdate("eventId")) {
            sb.setEventId(eventId);
        }
        if (isFieldUpdate("startTime")) {
            sb.setStartTime(startTime);
        }
        if (isFieldUpdate("forces")) {
            sb.setForces(forces);
        }
        if (isFieldUpdate("monsterLevel")) {
            sb.setMonsterLevel(monsterLevel);
        }
        if (isFieldUpdate("distance")) {
            sb.setDistance(distance);
        }
        if (isFieldUpdate("onewayTime")) {
            sb.setOnewayTime(onewayTime);
        }
        if (isFieldUpdate("totalForces")) {
            sb.setTotalForces(totalForces);
        }
        if (isFieldUpdate("eventQueueDbid")) {
            sb.setEventQueueDbid(eventQueueDbid);
        }
        if (isFieldUpdate("randomReward")) {
            sb.setRandomReward(randomReward);
        }
        if (isFieldUpdate("type")) {
            sb.setType(type);
        }
        if (isFieldUpdate("boxReward")) {
            sb.setBoxReward(boxReward);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getLocationId() {
        return locationId;
    }

    public void putLocationId(int locationId) {
        if (this.locationId != locationId) {
            this.locationId = locationId;
            updateRedisHashField(getKey(), "locationId", this.locationId);
        }
    }

    public void addLocationId(int value) {
        this.locationId += value;
        updateRedisHashField(getKey(), "locationId", this.locationId);
    }

    public int getEventId() {
        return eventId;
    }

    public void putEventId(int eventId) {
        if (this.eventId != eventId) {
            this.eventId = eventId;
            updateRedisHashField(getKey(), "eventId", this.eventId);
        }
    }

    public void addEventId(int value) {
        this.eventId += value;
        updateRedisHashField(getKey(), "eventId", this.eventId);
    }

    public long getStartTime() {
        return startTime;
    }

    public void putStartTime(long startTime) {
        if (this.startTime != startTime) {
            this.startTime = startTime;
            updateRedisHashField(getKey(), "startTime", this.startTime);
        }
    }

    public void addStartTime(long value) {
        this.startTime += value;
        updateRedisHashField(getKey(), "startTime", this.startTime);
    }

    public long getForces() {
        return forces;
    }

    public void putForces(long forces) {
        if (this.forces != forces) {
            this.forces = forces;
            updateRedisHashField(getKey(), "forces", this.forces);
        }
    }

    public void addForces(long value) {
        this.forces += value;
        updateRedisHashField(getKey(), "forces", this.forces);
    }

    public int getMonsterLevel() {
        return monsterLevel;
    }

    public void putMonsterLevel(int monsterLevel) {
        if (this.monsterLevel != monsterLevel) {
            this.monsterLevel = monsterLevel;
            updateRedisHashField(getKey(), "monsterLevel", this.monsterLevel);
        }
    }

    public void addMonsterLevel(int value) {
        this.monsterLevel += value;
        updateRedisHashField(getKey(), "monsterLevel", this.monsterLevel);
    }

    public int getDistance() {
        return distance;
    }

    public void putDistance(int distance) {
        if (this.distance != distance) {
            this.distance = distance;
            updateRedisHashField(getKey(), "distance", this.distance);
        }
    }

    public void addDistance(int value) {
        this.distance += value;
        updateRedisHashField(getKey(), "distance", this.distance);
    }

    public int getOnewayTime() {
        return onewayTime;
    }

    public void putOnewayTime(int onewayTime) {
        if (this.onewayTime != onewayTime) {
            this.onewayTime = onewayTime;
            updateRedisHashField(getKey(), "onewayTime", this.onewayTime);
        }
    }

    public void addOnewayTime(int value) {
        this.onewayTime += value;
        updateRedisHashField(getKey(), "onewayTime", this.onewayTime);
    }

    public int getFailNum() {
        return failNum;
    }

    public void putFailNum(int failNum) {
        if (this.failNum != failNum) {
            this.failNum = failNum;
            updateRedisHashField(getKey(), "failNum", this.failNum);
        }
    }

    public void addFailNum(int value) {
        this.failNum += value;
        updateRedisHashField(getKey(), "failNum", this.failNum);
    }

    public long getTotalForces() {
        return totalForces;
    }

    public void putTotalForces(long totalForces) {
        if (this.totalForces != totalForces) {
            this.totalForces = totalForces;
            updateRedisHashField(getKey(), "totalForces", this.totalForces);
        }
    }

    public void addTotalForces(long value) {
        this.totalForces += value;
        updateRedisHashField(getKey(), "totalForces", this.totalForces);
    }

    public long getEventQueueDbid() {
        return eventQueueDbid;
    }

    public void putEventQueueDbid(long eventQueueDbid) {
        if (this.eventQueueDbid != eventQueueDbid) {
            this.eventQueueDbid = eventQueueDbid;
            updateRedisHashField(getKey(), "eventQueueDbid", this.eventQueueDbid);
        }
    }

    public void addEventQueueDbid(long value) {
        this.eventQueueDbid += value;
        updateRedisHashField(getKey(), "eventQueueDbid", this.eventQueueDbid);
    }

    public String getRandomReward() {
        return randomReward;
    }

    public void putRandomReward(String randomReward) {
        if (!this.randomReward.equals(randomReward)) {
            this.randomReward = randomReward;
            updateRedisHashField(getKey(), "randomReward", this.randomReward);
        }
    }

    public int getType() {
        return type;
    }

    public void putType(int type) {
        if (this.type != type) {
            this.type = type;
            updateRedisHashField(getKey(), "type", this.type);
        }
    }

    public void addType(int value) {
        this.type += value;
        updateRedisHashField(getKey(), "type", this.type);
    }

    public String getBoxReward() {
        return boxReward;
    }

    public void putBoxReward(String boxReward) {
        if (!this.boxReward.equals(boxReward)) {
            this.boxReward = boxReward;
            updateRedisHashField(getKey(), "boxReward", this.boxReward);
        }
    }

}